<?php


namespace App\HttpController;
use EasySwoole\Mysqli\QueryBuilder;
use EasySwoole\Queue\Job;
use EasySwoole\Http\AbstractInterface\Controller;
use App\Models\AdminModel;
use App\Models\UserModel;
use EasySwoole\Component\Di;
use App\HttpController\UserBase;
use App\Models\Project;
use App\service\MyQueue;
use App\Models\Assign;
use App\Models\History;
use EasySwoole\ORM\DbManager;

class Owner  extends UserBase
{
    
        
    public function onRequest(?string $action): ?bool
    {
        print_r("owner check user login.....");
        if (!parent::onRequest($action)) {
            return false;
        }
       if(!in_array($this->who['type'],[2])){
        
               $this->writeJson(403,"u r not permitted!");
               return false;

       }
        return true;
    }


  function list(){
    $limit = \EasySwoole\EasySwoole\Config::getInstance()->getConf('pagelimit');
    $model = Project::create();
    $page = $this->input('page',1);
    $owner = $this->input("owner","");
    $lxnumber = $this->input("lxnumber","");
    $name = $this->input("name","");
    $wtdw = $this->input("wtdw","");
    $bzjg = $this->input("bzjg","");
    $addtime = $this->input("addtime","");
    $industry = $this->input("industry","");
    $wtdwcontactor = $this->input("wtdwlxr","");
    $pslb = $this->input("pslb","");
    $where = [];
    if($owner == 'null'){
        $owner = "";
    }

    if($industry == 'null' || $industry == "1"){
        $industry = "";
    }

    if($lxnumber == 'null'){
        $lxnumber = "";
    }
    if($name == 'null'){
        $name = "";
    }
    if($wtdw == 'null'){
        $wtdw = "";
    }
    if($bzjg == 'null'){
        $bzjg = "";
    }
    if($pslb == 'null'){
        $pslb = "";
    }
    if($wtdwcontactor == 'null'){
        $wtdwcontactor = "";
    }
   

    if($owner){
        $model = $model->where("owner","%".$owner."%",'like');
    }

    if($lxnumber){
   
        $model = $model->where("lxnumber","%".$lxnumber."%",'like');
    }
    if($name){
        $model = $model->where("name","%".$name."%",'like');
    }
    if($wtdw){
        $model = $model->where("wtdw","%".$wtdw."%",'like');
    }
    if($bzjg){
        $model = $model->where("bzjg","%".$bzjg."%",'like');
    }
    
    if($industry){
        $model = $model->where(["industry"=>$industry]);
    }

    if($pslb && $pslb!='1'){
        $model = $model->where(['approve_type'=>$pslb]);
    }
    if($wtdwcontactor){
        $model = $model->where("wtdwcontactor","%".$wtdwcontactor."%",'like');
    }

    
        $st = $this->input("st");
        if($st!="done"){
          $list = $model->where(['owner'=>$this->who['userAccount']])->where('status',4,'=');
        }else{
          $list = $model->where(['owner'=>$this->who['userAccount']])->where('status',4,'>');
        }
  
        $model = $model->order('id', 'DESC')->limit($limit * ($page - 1), $limit)->withTotalCount();
        $list = $model->all(null);
        $result = $model->lastQueryResult();
        $total = $result->getTotalCount();
        $pagecon = $this->showPageStringAsAJAXForitem("jumpage",$page,$limit,$total,$owner);
        $this->writeJson(200,['list'=>$list,'pg'=>$pagecon]);

  }


  function detail(){
    $data = $this->request()->getRequestParam();
    $id = intval($data['id']);
    $item  =Project::create()->get(['id'=>$id]);
    $logs = History::create()->join('user_list','user_list.id = approve_history.userid')->field(["approve_history.*","user_list.displayname"])->all(['itemid'=>$id]);
    
    for($i=0;$i<count($logs);$i++){
        $logs[$i]['file'] = \App\Models\FileApp::create()->all(['approveid'=>$logs[$i]['id']]);
    
    }
    $item["exptres"] =$logs;
    $this->writeJson(200,['item'=>$item,'logs'=>$logs]);
  }





  function dealitem(){
    $data = $this->request()->getRequestParam();
    $id = intval($data['id']);
    $item  =Project::create()->get(['id'=>intval($id)]);
    if ($item['status'] != 4) {
        $this->writeJson(500,['code'=>500,'msg'=>'error found...']);
        return false;
    }
    try {
        // 开启事务
        \EasySwoole\ORM\DbManager::getInstance()->startTransaction();
        if($data['appres']){
            $status = 5;
        }else{
            $status = -1;
        }
        // 执行更新 $model 的更新操作
        $this->logapprove($this->who['id'],$data['appres'],$id,$data['idea'],0,$this->who['type']);
        
        Project::create()->update(['status'=>$status],['id'=>$id]);
        // 提交事务
        \EasySwoole\ORM\DbManager::getInstance()->commit();


    $this->writeJson(200,['code'=>200],'ok to update');
    } catch(\Throwable  $e){

    $this->writeJson(500,['code'=>500,'msg'=>'error'],'ok to update');
        // 回滚事务
        var_dump($e->getMessage());
        \EasySwoole\ORM\DbManager::getInstance()->rollback();
    }

  }





   
} 
